<template>
  <div>
    <el-button @click="$router.push('/taskSystem')" type="success" >发布系统任务</el-button>
    <el-button @click="$router.push('/taskUser')" type="success" >发布用户任务</el-button>
  </div>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="id" prop="taskId" />
    <el-table-column label="用户" prop="userId" />
    <el-table-column label="任务标题" prop="taskTitle" />
    <el-table-column label="任务描述" prop="content" />
    <el-table-column label="标签" prop="tag" />
    <el-table-column label="任务类型" prop="taskType" />
    <el-table-column label="开始/通知时间" prop=" startTime" />
    <el-table-column label="结束时间" prop="endTimee" />
    <el-table-column label="任务优先级/重要程度" prop="priority" />
    <el-table-column label="任务状态" prop="status" />
    <el-table-column label="经验值" prop="experience" />
    <el-table-column label="提醒时间1" prop="remind1" />
    <el-table-column label="提醒时间2" prop="remind2" />
    <el-table-column label="提醒时间3" prop="remind3" />
    <el-table-column label="是否重复" prop="isRecurring" />
    <el-table-column label="任务创建时间" prop="createdAt" />
    <el-table-column label="任务更新时间" prop="updatedAt" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="搜索" />
      </template>
      <template #default="scope">
        <el-button size="small" type="primary" plain @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button
            size="small"
            type="danger"
            plain
            @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>

  </el-table>
  <template>
    <el-pagination background layout="prev, pager, next" :total="1000" />
  </template>
</template>


<script lang="ts" setup>
import {computed, getCurrentInstance, ref} from 'vue'
import {isBoolean} from "element-plus/es/utils";
import data from "@/views/Data.vue";
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Action } from 'element-plus'
const {appContext: {config: {globalProperties: global}}} = getCurrentInstance();//3.0.11

interface Task {
  taskId: number; //任务的id
  userId: number; //用户id
  taskTitle: string; //任务标题
  content: string; //任务描述
  tag: number; //任务的标签
  taskType:number; //任务类型（通知型、计时型）
  startTime: string; //开始/通知时间
  endTime: string; //结束时间
  priority: number; //任务优先级/重要程度
  status: number; //任务状态（进行中、未完成、完成）
  experience: number; //任务完成后奖励的经验值
  remind1: string; //提醒时间1（如按时提醒、提前5分钟提醒）
  remind2: string; //提醒时间2
  remind3: string; //提醒时间3
  isRecurring: number; //是否重复
  createdAt: string; //任务创建时间
  updatedAt: string; //任务更新时间
}

const search = ref('')
const filterTableData = computed(() =>
    tableData.value.filter(
        (data) =>
            !search.value ||
            data.taskTitle.toLowerCase().includes(search.value.toLowerCase())
    )
)
const handleEdit = (index: number, row: Task) => {
  console.log(index, row)
}
const handleDelete = (index: number, row: Task) => {
  global.$request.post('/task/deleteTask',row).then(res => {
    getAllTasks();
  })
}

const tableData = ref<Task[]>([]) // 使用 ref 创建响应式数组

getAllTasks();
console.log(tableData)
function getAllTasks(){
  global.$request.get("/task/getAllTasks").then(res => {
    //注释
    tableData.value = res.data;
  })
}


</script>